package AWT.事件监听;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class dom2 {
    public static void main(String[] args) {
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        //可以自定义触发会返回的命令，如果没有定义，会定义默认值
//        button1.setActionCommand("button1-start");
        button2.setActionCommand("button2-stop");

        Mymonitor mymonitor = new Mymonitor();
        button1.addActionListener(mymonitor);
        button2.addActionListener(mymonitor);

        frame.setLayout(new GridLayout(2,1));
        frame.add(button1);
        frame.add(button2);
        frame.pack();//自适应
        frame.setVisible(true);
    }
}

class Mymonitor implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand()获得按钮信息（默认按钮名字）
        System.out.println("按钮被点击");
//        System.out.println(e.getActionCommand());
        if (e.getActionCommand().equals("start")) {
            System.out.println("开始");
        }else {
            System.out.println("停止");
        }
    }
}